package cn.taobaoke.restful.domain;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * ProductRepository类.
 * +-----------------------------------------------------------------------------------------------+
 * |类名称: ProductRepository                                                                         |
 * |创建时间: 3/17/17                                                                               |
 * |功能简介:                                                                                       |
 * |参考链接:                                                                                       |
 * +-----------------------------------------------------------------------------------------------+
 *
 * @author luch
 */
public interface ProductRepository extends JpaRepository<ProductEntity, Long> {

    Optional<ProductEntity> findByIdAndDomainId(Long id, Long domainId);

    Optional<ProductEntity> findByItemIdAndDomainId(String itemId, Long domainId);

    @Query(value = "SELECT * FROM tb_product WHERE domain_id = :domainId ORDER BY id ASC LIMIT :start, :n ", nativeQuery = true)
    List<ProductEntity> findLimitN(@Param("start") Integer start, @Param("n") Integer cnt, @Param("domainId") Long domainId);

    List<ProductEntity> findByDomainIdAndItemIdIn(Long domainId, Collection<String> itemIds);

    /**
     * 模糊查询
     *
     * @param keyword
     * @param pageable
     * @return
     * @note 注意pageable设置倒排序
     */
    @Query("from ProductEntity product where (product.itemName like %:keyword% or product.shopName like %:keyword%) and product.domainId = :domainId")
    Page<ProductEntity> findByItemNameOrShopNameLike(@Param("keyword") String keyword, @Param("domainId") Long domainId, Pageable pageable);


    /**
     * 两个关键字的查询
     *
     * @param keyword1
     * @param keyword2
     * @param pageable
     * @return
     */
    @Query("from ProductEntity product where (product.itemName like %:keyword1% or product.itemName like %:keyword2% or product.shopName like %:keyword1% or product.shopName like %:keyword2% ) and product.domainId = :domainId")
    Page<ProductEntity> findByItemNameOrItemNameLike(@Param("keyword1") String keyword1, @Param("keyword2") String keyword2, @Param("domainId") Long domainId, Pageable pageable);


    /**
     * 目前最多三个关键字的查询
     * TODO 后续需要进行优化处理, 多个关键字, 模糊查询, 比如es那种查询方式
     *
     * @param keyword1
     * @param keyword2
     * @param keyword3
     * @param pageable
     * @return
     */
    @Query("from ProductEntity product where (product.itemName like %:keyword1% or product.itemName like %:keyword2%  or product.itemName like %:keyword3% or product.shopName like %:keyword1% or product.shopName like %:keyword2% or product.shopName like %:keyword3% ) and product.domainId = :domainId")
    Page<ProductEntity> findByItemNameOrItemNameOrItemNameLike(@Param("keyword1") String keyword1, @Param("keyword2") String keyword2, @Param("keyword2") String keyword3, @Param("domainId") Long domainId, Pageable pageable);
}
